package S010;

import java.sql.Connection;
import java.sql.DriverManager;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class Task {
    private int TaskID;//任务编号，数据库自增
    private String TaskTheme;//任务主题    
    private String TaskIssuTime;//任务发布日期  
    private String TaskDeadline;//任务截止日期  
    private String UserID;//任务发布人ID,数据库中可考虑在表名中存放 
    private String TaskGroup;//任务所属组,使用InviteID关联对应   
    /*TaskContent属性*/
    private String TaskContent;//任务内容
    private String CompleteStatus;//完成度
    
    public Connection GetConnection() {//与数据库建立连接
        String url = "jdbc:mysql://localhost:3306/listsystem";//与本机的3306端口建立连接，进入数据库listsystem
        String driverName = "com.mysql.jdbc.Driver";//驱动注册
        String userName = "root";
        String password = "root";
        Connection conn = null;//用于建立与数据库的连接
        try{
            Class.forName(driverName);// jdbc4.0 后无需使用这句进行驱动注册操作 
            conn = DriverManager.getConnection(url, userName, password);
        }catch(Exception e){
            System.out.println(e);
        }
        return conn;
    }
    
    public Task(String taskTheme,String taskDeadline,/*String userID,*/String taskGroup,String taskContent,String completeStatus) {
        //setTaskID(1);
        setTaskTheme(taskTheme);
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");//设置日期格式
        setTaskIssuTime(df.format(new Date()));//获得当前日期
        setTaskDeadline(taskDeadline);
        //setUserID(userID);
        setTaskGroup(taskGroup);
        setTaskContent(taskContent);
        if(completeStatus==null)
            setCompleteStatus(null);
        else
            setCompleteStatus(completeStatus);
        Connection conn = GetConnection();
        PreparedStatement taskPS = null;
        String taskSql = "insert into task_userid values(null,?,?,?,?)";//新建任务
        if(conn != null)
            try {
                taskPS = conn.prepareStatement(taskSql);
                if(taskPS != null) {
                    taskPS.setString(1, getTaskTheme());
                    taskPS.setString(2, getTaskIssuTime());
                    taskPS.setString(3, getTaskDeadline());
                    taskPS.setString(4, getTaskGroup());
                    taskPS.execute();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }finally {
                try {
                    if(taskPS != null)
                        taskPS.close();
                    if(conn != null)
                        conn.close();
                    taskPS = null;
                    conn = null;
                } catch (SQLException e) {
                    e.printStackTrace();
                }
        }
        conn = GetConnection();
        PreparedStatement getTaskIDPS = null;
        ResultSet getTaskIDRS = null;//查询获取任务ID号
        String getTaskIDSql = "select TaskID from task_userid where TaskTheme=? and TaskIssuTime=? and TaskDeadline=? and TaskGroup=?";
        if(conn != null)
            try {
                getTaskIDPS = conn.prepareStatement(getTaskIDSql);
                if(getTaskIDPS != null) {
                    getTaskIDPS.setString(1, getTaskTheme());
                    getTaskIDPS.setString(2, getTaskIssuTime());
                    getTaskIDPS.setString(3, getTaskDeadline());
                    getTaskIDPS.setString(4, getTaskGroup());
                    getTaskIDRS = getTaskIDPS.executeQuery();
                    if(getTaskIDRS != null && getTaskIDRS.next()) {
                        TaskID = Integer.parseInt(getTaskIDRS.getString("TaskID"));
                        setTaskID(TaskID);
                    }
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }finally {
                try {
                    if(getTaskIDRS != null)
                        getTaskIDRS.close();
                    if(getTaskIDPS != null)
                        getTaskIDPS.close();
                    if(conn != null)
                        conn.close();
                    getTaskIDRS = null;
                    getTaskIDPS = null;
                    conn = null;
                } catch (SQLException e) {
                    e.printStackTrace();
                }
        }
        conn = GetConnection();
        PreparedStatement contentPS = null;//记录任务详细内容
        String contentSql = "insert into taskcontent_userid values(?,?,?)";
        if(conn != null)
            try {
                contentPS = conn.prepareStatement(contentSql);
                if(contentPS != null) {
                    contentPS.setInt(1, getTaskID());
                    contentPS.setString(2, getTaskContent());
                    contentPS.setString(3, getCompleteStatus());
                    contentPS.execute();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }finally {
               try {
                   if(contentPS != null)
                       contentPS.close();
                   if(conn != null)
                       conn.close();
                   contentPS = null;
                   conn = null;
                } catch (SQLException e) {
                   e.printStackTrace();
                }
            }
    }
    
    public String getTaskContent() {
        return TaskContent;
    }
    public void setTaskContent(String taskContent) {
        TaskContent = taskContent;
    }
    public String getCompleteStatus() {
        return CompleteStatus;
    }
    public void setCompleteStatus(String completeStatus) {
        CompleteStatus = completeStatus;
    }
    public int getTaskID() {
        return TaskID;
    }
    public void setTaskID(int taskID) {
        TaskID = taskID;
    }
    public String getTaskTheme() {
        return TaskTheme;
    }
    public void setTaskTheme(String taskTheme) {
        TaskTheme = taskTheme;
    }
    public String getTaskIssuTime() {
        return TaskIssuTime;
    }
    public void setTaskIssuTime(String taskIssuTime) {
        TaskIssuTime = taskIssuTime;
    }
    public String getTaskDeadline() {
        return TaskDeadline;
    }
    public void setTaskDeadline(String taskDeadline) {
        TaskDeadline = taskDeadline;
    }
    public String getUserID() {
        return UserID;
    }
    public void setUserID(String userID) {
        UserID = userID;
    }
    public String getTaskGroup() {
        return TaskGroup;
    }
    public void setTaskGroup(String taskGroup) {
        TaskGroup = taskGroup;
    }

    @Override
    public String toString() {
        return "Task [TaskID=" + TaskID + ", TaskTheme=" + TaskTheme + ", TaskIssuTime=" + TaskIssuTime
                + ", TaskDeadline=" + TaskDeadline + ", UserID=" + UserID + ", TaskGroup=" + TaskGroup
                + ", TaskContent=" + TaskContent + ", CompleteStatus=" + CompleteStatus + "]";
    }
}
